package com.hanyao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanyao.reggie.bean.Employee;
import com.hanyao.reggie.common.R;
import com.hanyao.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author 寒钥
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();

        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lambdaQueryWrapper);
        if(emp == null){
            return R.error("登入失败");
        }
        if (!s.equals(emp.getPassword())){
            return R.error("登入失败");
        }
        if(emp.getStatus()==0){
            return R.error("用户被禁用");
        }
        request.getSession().setAttribute("id",emp.getId());
        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logeOut(HttpServletRequest request){
        request.getSession().removeAttribute("id");
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        //    Employee(id=null, username=2323242323, name=234234,
        //    password=null, phone=13222222222, sex=1, idNumber=111111111111111111,
        //    status=null, createTime=null, updateTime=null,
        //    createUser=null, updateUser=null)
        Long id = (Long)request.getSession().getAttribute("id");
        employee.setStatus(1);
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setCreateUser(id);
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(id);
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        employeeService.save(employee);
        return R.success("添加员工成功");
    }
    @GetMapping("/page")
    public R<Page> list(int page,int pageSize,String name){
        //首先创建分页对象
        Page<Employee> employeePage = new Page<>(page, pageSize);
        //创建过滤条件
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        //创建排序条件
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        //创建查询事务
        employeeService.page(employeePage,lambdaQueryWrapper);
        return R.success(employeePage);
    }
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        Long id = employee.getId();
        Long updateName = (Long)request.getSession().getAttribute("id");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(updateName);
        //LambdaUpdateWrapper<Employee> updateWrapper = Wrappers.lambdaUpdate();
        //updateWrapper.eq(Employee::getId,id);
        //employeeService.update(employee,updateWrapper);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("收到id={}",id);
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
